package com.mls.fivedistrict.controller.backend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.pojo.Application;
import com.mls.fivedistrict.server.ApplicationService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 */
@RestController()
@RequestMapping("/api/manager")
public class ApplicationController extends BaseController {
    @Autowired
    ApplicationService applicationService;

    @GetMapping("/v1/applications")
    public ServiceResponse getAdversingList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List<Application> applicationList = applicationService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo(applicationList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }


    @PostMapping("/v1/application")
    public ServiceResponse addApplication(@RequestBody Application application) {
        BeanValidator.check(application);
        boolean insert = applicationService.insert(application);
        if (!insert) {
            return ServiceResponse.createErrorResponse("写入失败");
        }
        return ServiceResponse.createSuccessResponse("写入成功");
    }

    @PutMapping("/v1/application/{applicationId}")
    public ServiceResponse eidtApplication(@PathVariable Long applicationId, @RequestBody Application application) {
        application.setId(applicationId);
        boolean update = applicationService.updateById(application);
        if (!update) {
            return ServiceResponse.createErrorResponse("编辑失败");
        }
        return ServiceResponse.createSuccessResponse("编辑成功");
    }


    @GetMapping("/v1/application/{applicationId}")
    public ServiceResponse eidtAdvertising(@PathVariable Long applicationId) {
        Application application = applicationService.selectById(applicationId);
        if (application != null) {
            return ServiceResponse.createSuccessResponse(application);
        }
        return ServiceResponse.createErrorResponse("广告不存在");
    }

    @DeleteMapping("/v1/application/{applicationId}")
    public ServiceResponse deleteApplication(@PathVariable Long applicationId) {
        Application application = new Application();
        application.setId(applicationId);
        application.setDelete(true);
        boolean update = applicationService.updateById(application);
        if (!update) {
            return ServiceResponse.createErrorResponse("编辑失败");
        }
        return ServiceResponse.createSuccessResponse("删除成功");
    }


}
